// use/entities/user.entity.ts
import { Column, Entity, PrimaryGeneratedColumn, BeforeInsert, OneToOne, ManyToOne, OneToMany } from 'typeorm';
import * as bcrypt from 'bcryptjs';
import { Role } from '../../role/entities/role.entity'; // Assuming you have a Role entity
import { Company } from '../../company/entities/company.entity'; // Assuming you have a Company entity
import { Dept } from 'src/modules/system/dept/entities/dept.entity';
import { Duty } from 'src/modules/system/duty/entities/duty.entity';
import { UserError } from 'src/modules/questionbank/user_error/entities/user_error.entity';
@Entity('user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  Id: string;
 
  @Column({ length: 100 })
  UserName: string; // 用户名
 
  @Column({ length: 100 })
  Password: string;  // 密码

  @Column({ length: 100, nullable: true })
  RealName: string;  //真实姓名

  @Column({ length: 100, nullable: true })
  Phone: string;  // 手机号

  @Column({ length: 100, nullable: true })
  Avatar: string;  // 头像

  @ManyToOne(() => Company, (Company) => Company.Users)
  Company: Company; // 关联角色实体

  @ManyToOne(() => Dept, (Dept) => Dept.Users)
  Dept: Dept; // 关联角色实体

  @ManyToOne(() => Duty, (Duty) => Duty.Users)
  Duty: Duty; // 关联角色实体

  @ManyToOne(() => Role, (Role) => Role.Users)
  Role: Role; // 关联角色实体

  @OneToMany(() => UserError, UserError => UserError.user)
  errorLinks: UserError[];
  
  @Column()
  Status: number; // 用户状态，1为正常，0为禁用
 
  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  CreateTime: Date;
 
  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  UpdateTime: Date;
  
  @BeforeInsert() 
  async encryptPwd() { 
    this.Password = await bcrypt.hashSync(this.Password); 
  } 
  
}